// pages/UserOrder/UserOrder.js
let OPENID = ''
Page({
  data: {
    show:true,
    show2:false,
    show3:false,
    show4:false,
  All:[],
  Sell:[],
  Not:[],
  Unused:[],
  index:1
  },
  onLoad(options) {
  let ty = options.ty
  OPENID = options.openid
    console.log('ty:',ty)
    console.log('OPENID:',OPENID)
    this.getUserOrder(1)
    this.getUserOrder(2)
    this.getUserOrder(3)
    this.getUserOrder(4)
  },
  getUserOrder(T){
    wx.cloud.database().collection('All-Orders')
    .where({
      openid:OPENID
    }).get()
    .then(res=>{
      console.log('------------------获取用户订单成功--------------------')
      if(T==1){
      // 全部订单
      var AllID = []
      for (let index = 0; index < res.data[0].Sell.SellID.length; index++) {
        AllID.push(res.data[0].Sell.SellID[index])
      }
      for (let index = 0; index < res.data[0].Not.NotID.length; index++) {
        AllID.push(res.data[0].Not.NotID[index])
      }
      for (let inde = 0; inde < res.data[0].Unused.UnusedID.length; inde++) {
        AllID.push(res.data[0].Unused.UnusedID[inde])
      }
      for (let index = 0; index < AllID.length; index++) {
        this.getOneOrder(AllID[index],1)
      }
      console.log('------------------获取用户订单成功--------------------')
      }else if(T==2){
      //  已卖出
      var SellID = []
      for (let index = 0; index < res.data[0].Sell.SellID.length; index++) {
        SellID.push(res.data[0].Sell.SellID[index])
      }
      for (let index = 0; index < SellID.length; index++) {
        this.getOneOrder(SellID[index],2)
      }
      }else if(T==3){
      // 未收货
      var NotID = []
      for (let index = 0; index < res.data[0].Not.NotID.length; index++) {
        NotID.push(res.data[0].Not.NotID[index])
      }
      for (let index = 0; index < NotID.length; index++) {
        this.getOneOrder(NotID[index],3)
      }
      }else{
      // 待使用
      var UnusedID = []
      for (let index = 0; index < res.data[0].Unused.UnusedID.length; index++) {
        UnusedID.push(res.data[0].Unused.UnusedID[index])
      }
      for (let index = 0; index < UnusedID.length; index++) {
        this.getOneOrder(UnusedID[index],4)
      }
      }

    })
    .catch(err=>{
     console.log('获取用户订单失败',err)
    })
   },
   getOneOrder(ID,T2){
    wx.cloud.database().collection('Order-information')
    .where({
      orderNum:ID
    }).get()
    .then(res=>{
      if(T2==1){
        this.setData({
          All:this.data.All.concat(res.data[0]) 
        })
      } else if(T2==2){
        this.setData({
          Sell:this.data.Sell.concat(res.data[0]) 
        })
      } else if(T2==3){
        this.setData({
          Not:this.data.Not.concat(res.data[0]) 
        })
      } else {
        this.setData({
          Unused:this.data.Unused.concat(res.data[0]) 
        })
      }

    })
    .catch(err=>{
      console.log('获取用户订单失败',err)
    })
   },
   changePage(e){
     let INDEX
    console.log(e.currentTarget.dataset.index)
    INDEX = e.currentTarget.dataset.index
    if(INDEX==1){
      this.setData({
        show:true,
        show2:false,
        show3:false,
        show4:false,
      })
    }else if(INDEX==2){
      this.setData({
        show:false,
        show2:true,
        show3:false,
        show4:false,
      })
    }else if(INDEX==3){
      this.setData({
        show:false,
        show2:false,
        show3:true,
        show4:false,
      })
    }else{
      this.setData({
        show:false,
        show2:false,
        show3:false,
        show4:true,
      })
    }
    this.setData({
      index: e.currentTarget.dataset['index'],
    })
   },
   goOrder(e){
     console.log(e.currentTarget.dataset.orderid)
     wx.navigateTo({
       url: '/pages/OrderInformation/OrderInformation?orderNum='+e.currentTarget.dataset.orderid,
     })
   }
})